/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.cd.impl;

import java.awt.Color;

import javax.swing.ImageIcon;

import org.jdom.Element;

import who.tmap.bo.BizObject;
import who.tmap.bo.cd.CDPointSymbols;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.point.CDPointDataDrivenSize;
import who.tmap.common.TMapException;
import who.tmap.dao.TMapDAOException;

public class CDPointDataDrivenSizeDAOImpl extends BaseCartographicDisplayDAOImpl {

	CDPointDataDrivenSizeDAOImpl() {
		super();
	}
	
	public BizObject loadXml(Element elt) throws TMapDAOException {
		CDPointDataDrivenSize cd =null;
	
		try {
			cd =(CDPointDataDrivenSize)getCD( elt );
			Element mainElt =elt.getChild( "PointDataDrivenSize" );
			
			cd.setJoinIdColumnName( "" );
			cd.setDataColumnName( "" );
			
			cd.setXColumn( getNullStringIfEmpty( mainElt.getAttributeValue( "xColumnName" ) ) );
			cd.setYColumn( getNullStringIfEmpty( mainElt.getAttributeValue( "yColumnName" ) ) );
			cd.setDescriptorColumn( mainElt.getAttributeValue( "descriptorColumnName" ) );	
			cd.setScalingFactor( mainElt.getAttribute( "scalingFactor" ).getIntValue() );			
			
			cd.setCDPointType( CDTypes.POINT_DATA_DRIVEN_SIZE );
			
			int symbolId =-1;
			String symbolName =mainElt.getAttributeValue( "shape" );
			
			for( int ct =0; ct < CDPointSymbols.names.length; ct++ ) {
				if ( CDPointSymbols.names[ ct ].equalsIgnoreCase( symbolName ) ) {
					symbolId =ct;
					break;
				}
			}		
			
			if ( symbolId == -1 ) {
				throw new TMapException( "Invalid shape name [" + symbolName + "]" );
			}
			
			cd.setSymbol( symbolId );
			if ( symbolId == CDPointSymbols.BITMAP ) {
	    		byte[] imageData =miscHelper.loadInlineImage( mainElt.getChild( "InlineBitmap" ) );
	    		
	    		cd.setImageBytes( imageData );
	        	ImageIcon imageIcon =new ImageIcon( imageData );
	        	cd.setImage( imageIcon.getImage() );
			}			
			
			cd.setFillPattern( mainElt.getAttribute( "fillPattern" ).getIntValue() );			
			
			cd.setColor( miscHelper.rgbToColor( mainElt.getAttributeValue( "color") ) );
			
			cd.setSizeColumn( mainElt.getAttributeValue( "sizeColumnName" ) );
			
			
		} catch( Exception e ) {
			throw new TMapDAOException( e );
		}
		
		
		return cd;
	}

	public Element saveXml(BizObject object) throws TMapDAOException {
		CDPointDataDrivenSize cd =(CDPointDataDrivenSize)object;
		
        Element rootElt =super.saveXml( object );
        Element mainElt =new Element( "PointDataDrivenSize" );
        rootElt.addContent( mainElt );
        
        mainElt.setAttribute( "version", VERSION );
        
        mainElt.setAttribute( "xColumnName", cd.getXColumn() ); 
        mainElt.setAttribute( "yColumnName", cd.getYColumn() );  
        mainElt.setAttribute( "descriptorColumnName", cd.getDescriptorColumn() );
        mainElt.setAttribute( "scalingFactor", Integer.toString( cd.getScalingFactor() ) );        
        
        mainElt.setAttribute( "shape", CDPointSymbols.names[ cd.getSymbol() ] );
        mainElt.setAttribute( "fillPattern", Integer.toString( cd.getFillPattern() ) );        
        mainElt.setAttribute( "color", miscHelper.colorToHexa( cd.getColor() != null 
			        														   ? cd.getColor() 
			        														   : Color.BLACK ) ); 
        mainElt.setAttribute( "sizeColumnName", cd.getSizeColumn() );
        
		if ( cd.getSymbol() == CDPointSymbols.BITMAP ) {
	        Element inlineElt =new Element( "InlineBitmap" );
	        mainElt.addContent( inlineElt );
	        inlineElt.setText( miscHelper.saveInlineImage( cd.getImageBytes() ) );
		}        
        
		return rootElt;
	}

}
